﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PokemonBattle.PokemonData.Util;

namespace PokemonBattle.PokemonData.SQLite.DAL
{
    public static class DALPokemonData
    {
        public static List<PokemonData> GetByNumber(int number)
        {
            string sql = "select * from V_PmData where number=" + number;
            var dt = DbHelperSQLite.ExecuteDataTable(sql);
            var list = from DataRow dr in dt.Rows
                       select DataRowToModel(dr);
            return list.ToList();
        }

        public static PokemonData GetModel(int pmId)
        {
            string sql = "select * from V_PmData where pmid=" + pmId;
            var dt = DbHelperSQLite.ExecuteDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                var dr = dt.Rows[0];
                return DataRowToModel(dr);
            }
            return null;
        }

        public static PokemonData DataRowToModel(DataRow row)
        {
            var data = new PokemonData();

            data.Identity = row["pmid"].ToString().ToInt();
            data.Number = row["number"].ToString().ToInt();
            data.NameBase = row["NameBase"].ToString();
            data.Trait1 = (Trait)row["Trait1"].ToString().ToInt();
            data.Trait2 = (Trait)row["Trait2"].ToString().ToInt();
            data.EggGroup1 = (EggGroup)row["EggGroup1"].ToString().ToInt();
            data.EggGroup2 = (EggGroup)row["EggGroup2"].ToString().ToInt();
            data.GenderRestriction = (PokemonGenderRestriction)row["GenderRestriction"].ToString().ToInt();
            data.Weight = row["Weight"].ToString().ToDouble();
            data.Bv.Hp = row["HpBase"].ToString().ToInt();
            data.Bv.Atk = row["AtkBase"].ToString().ToInt();
            data.Bv.Def = row["DefBase"].ToString().ToInt();
            data.Bv.Speed = row["SpeedBase"].ToString().ToInt();
            data.Bv.SpAtk = row["SpAtkBase"].ToString().ToInt();
            data.Bv.SpDef = row["SpDefBase"].ToString().ToInt();
            data.Type1 = row["Type1"].ToString();
            data.Type2 = row["Type2"].ToString();
            data.BeforeEvolution = row["BeforeEvolution"].ToString().ToInt();

            data.FrontImage = row["FrontImage"].ToString().ToLong();
            data.FrontImageF = row["FrontImageF"].ToString().ToLong();
            data.BackImage = row["BackImage"].ToString().ToLong();
            data.BackImageF = row["BackImageF"].ToString().ToLong();
            data.Icon = row["Icon"].ToString().ToLong();
            data.Frame = row["Frame"].ToString().ToLong();
            data.FrameF = row["FrameF"].ToString().ToLong();

            data.UsageCount = row["UsageCount"].ToString().ToInt();
            data.Moves = DALMoveLearnData.GetByPmId(data.Identity);
            data.AfterEvolution = GetAfterEvolution(data.Identity);
            return data;
        }

        public static List<int> GetAfterEvolution(int pmId)
        {
            string sql = "select AfterEvolution from V_AfterEvolution where pmid=" + pmId;
            var dt = DbHelperSQLite.ExecuteDataTable(sql);

            if (dt != null && dt.Rows.Count > 0)
            {
                return (from DataRow dr in dt.Rows select dr[0].ToString().ToInt()).ToList();
            }
            return new List<int>();
        }
    }
}
